home *** CD-ROM | disk | FTP | other *** search
- #!/usr/bin/perl
-
- # Server side include documentation at NCSA:
- # <URL:http://hoohoo.ncsa.uiuc.edu/docs/tutorials/includes.html>
- # FakeSSI documentation:
- # <URL:http://sw.cse.bris.ac.uk/WebTools/fakessi.html>
-
- # These lines (in httpd.conf) make the whole thing work!!!
- # Change the directory to point at your own /cgi-bin/
- # Exec /*.shtml /users/www/cgi-bin/fakessi.pl
- # The line above is the PREFERRED usage. It only parses files with the
- # extension of .shtml
-
- # Alternatively, all files are parsed using the following two lines.
- # Exec /* /users/www/cgi-bin/fakessi.pl
- # Exec /~* /users/www/cgi-bin/fakessi.pl
-
- # Configuration area.
- # $BINDIR is the /cgi-bin/ directory.
- $BINDIR='/users/ccnjb/cgi-bin';
-
- # $DOCROOT is the Document root (easy really, innit??)
- $DOCROOT='/users';
-
- # $INDEX is the name of the directory index file.
- $INDEX='index.html';
-
- # $USER_DIR is the name of the user directory.
- $USER_DIR='public_html';
-
- $errno=0;
-
- # Set the default error message you want, the size format, time format and
- # timezone here.
- $errmsg='[an error occurred while processing this directive]';
- $sizefmt='bytes';
- $timefmt="%A, %d-%b-%y %H:%M:%S %Z";
- $timezone='BST';
-
- # OK, now to work!!!
- print("Content-type: text/html\n\n");
-
- # Convert the target file name from WWW form into explicit form
- $sent=$ENV{SCRIPT_NAME};
- $ENV{'HTTP_REFERER'}=$sent;
- $infile=$sent;
- &MakePathname;
- $target=$outfile;
-
- # Read in target WWW page, and make into one long line.
- open(FOO,$target);
- @lines = <FOO>;
- foreach $line (@lines) {
- $bigline=join('',$bigline,$line);
- }
- close(FOO);
-
- # Go thru the line until you reach the end looking for SSI's.
- $len=length($bigline);
- while ($len > 0)
- {
- if ($bigline =~ /<!--#/ )
- {
- print($`);
- if ($' =~ /-->/)
- {
- $ssi = $`;
- $bigline = $';
- &HandleSSI;
- $len=length($bigline);
- }
- }
- else
- {
- $len = 0;
- print($bigline);
- }
- }
-
- sub HandleSSI {
- if ($ssi =~ /config/i)
- {
- @var1=split('="',$ssi);
- @var2=split('"',$var1[1]);
- $var=$var2[0];
- if ($ssi =~ /errmsg/i)
- {
- $errmsg=$var;
- }
- elsif ($ssi =~ /sizefmt/i)
- {
- $sizefmt=$var;
- }
- elsif ($ssi =~ /timefmt/i)
- {
- $timefmt=$var;
- }
- else
- {
- &GiveErrMsg;
- }
- }
- elsif ($ssi =~ /echo/i)
- {
- @var1=split('var="',$ssi);
- @var2=split('"',$var1[1]);
- $var=$var2[0];
- if ($var =~ /DOCUMENT_NAME/)
- {
- @out=split('/',substr($target,rindex($target,'/')));
- print("$out[1]\n");
- }
- elsif ($var =~ /DOCUMENT_URI/)
- {
- @output=split($DOCROOT,$target);
- print($output[1]);
- }
- elsif ($var =~ /DATE_GMT/)
- {
- $timetype = 0;
- $nowtime=time();
- &strftime;
- }
- elsif ($var =~ /DATE_LOCAL/)
- {
- $timetype = 1;
- $nowtime=time();
- &strftime;
- }
- elsif ($var =~ /LAST_MODIFIED/)
- {
- $timetype = 1;
- ($t,$t,$t,$t,$t,$t,$t,$t,$t,$nowtime,$t,$t,$t)=stat($target);
- &strftime;
- }
- else
- {
- &GiveErrMsg;
- }
- }
- elsif ($ssi =~ /exec/i)
- {
- if ($ssi =~ /cgi=/i)
- {
- @var1=split('cgi="',$ssi);
- @var2=split('"',$var1[1]);
- $var=$var2[0];
- $infile=$var;
- &MakePathname;
- $var=$outfile;
- if ($errno == 0)
- {
- open (FOO, "-|") || exec ($var);
- @pages = <FOO>;
- foreach $page (@pages) {
- print($page);
- }
- }
- }
-
- # Deliberate hold on CMD= tag ATM...
- # elsif ($ssi =~ /cmd=/i)
-
- }
- elsif ($ssi =~ /include/i)
- {
- &WhichFile;
- if ($errno == 0)
- {
- open(FOO,$filename);
- @lines = <FOO>;
- foreach $line (@lines) {
- print("$line");
- }
- }
- else
- {
- &GiveErrMsg;
- }
- }
- elsif ($ssi =~ /flastmod/i)
- {
- &WhichFile;
- if ($errno == 0)
- {
- ($t,$t,$t,$t,$t,$t,$t,$t,$t,$nowtime,$t,$t,$t)=stat($filename);
- $timetype=1;
- &strftime;
- }
- else
- {
- &GiveErrMsg;
- }
- }
- elsif ($ssi =~ /fsize/i)
- {
- &WhichFile;
- if ($errno == 0)
- {
- ($t,$t,$t,$t,$t,$t,$t,$size,$t,$t,$t,$t,$t)=stat($filename);
- if ($sizefmt =~ /abbrev/i)
- {
- print(int(($size / 1024) + 1),"Kbytes");
- }
- else
- {
- print("$size bytes");
- }
-
- }
-
- }
- else
- {
- &GiveErrMsg;
- }
- }
-
- sub MakePathname {
- $errno=1;
- $info=$infile;
- if ($info =~ /^\/cgi-bin\//)
- {
- @split1=split(/\/cgi-bin\//,$info);
- $info=join('/',$BINDIR,$split1[1]);
- }
- elsif ($info =~ /~/)
- {
- @split1=split(/~/,$info);
- if ($split1[1] =~ /\//)
- {
- $user=$`;
- ($t1,$t1,$t1,$t1,$t1,$t1,$t1,$dir,$t1)=getpwnam($user);
- $dir=join('/',$dir,$USER_DIR);
- $file=join('/',$dir,$');
- $info=$file;
- }
- }
- else
- {
- $new=join('',$DOCROOT,$info);
- $info=$new;
- }
-
- if ($info =~ /\/$/)
- {
- if (-d $info)
- {
- $file=join('',$info,$INDEX);
- $info=$file;
- }
- }
-
- $outfile=$info;
- if (!-e $outfile)
- {
- &GiveErrMsg;
- }
- else
- {
- $errno=0;
- }
- }
-
- sub GiveErrMsg {
- print("$errmsg");
- }
-
- sub WhichFile {
- $errno=1;
- if ($ssi =~ /virtual="/i)
- {
- $temp=$';
- $temp =~ /"/;
- $filename = $`;
- }
- elsif ($ssi =~ /file="/i)
- {
- $temp=$';
- $temp =~ /"/;
- $filename =join('/',substr($target,0,rindex($target,'/')),$`);
- }
- if (-e $filename)
- {
- $errno = 0;
- }
- }
-
- sub strftime {
- # Setup day and month names for later use.
- @sday=('Mon','Tue','Wed','Thu','Fri','Sat','Sun');
- @lday=('Monday','Tuesday','Wednesday',
- 'Thursday','Friday','Saturday','Sunday');
- @smon=('Jan','Feb','Mar','Apr','May','Jun',
- 'Jul','Aug','Sep','Oct','Nov','Dec');
- @lmon=('January','February','March','April','May','June',
- 'July','August','September','October','November','December');
-
- # Get time units
- if ($timetype == 0)
- {
- ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=gmtime($nowtime);
- }
- else
- {
- ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($nowtime);
- }
-
- # Print time
- $timestr=$timefmt;
- $len=length($timestr);
-
- while ($len > 0)
- {
-
- $extra='';
-
- if ($timestr =~/^%a/)
- {
- print("$sday[$day]");
- }
- elsif ($timestr =~/^%A/)
- {
- print("$lday[$day]");
- }
- elsif ($timestr =~/^%b/)
- {
- print("$smon[$mon]");
- }
- elsif ($timestr =~/^%B/)
- {
- print("$lmon[$mon]");
- }
- elsif ($timestr =~/^%d/)
- {
- print("$mday");
- }
- elsif ($timestr =~/^%H/)
- {
- print("$hour");
- }
- elsif ($timestr =~/^%I/)
- {
- $thour=$hour;
- if ($thour > 12)
- {
- $thour = $thour - 12;
- }
- print("$thour");
- }
- elsif ($timestr =~/^%j/)
- {
- print("$yday");
- }
- elsif ($timestr =~/^%m/)
- {
- print("$mon");
- }
- elsif ($timestr =~/^%M/)
- {
- print("$min");
- }
- elsif ($timestr =~/^%p/)
- {
- if ($hour > 11)
- {
- print("pm");
- }
- else
- {
- print("am");
- }
- }
- elsif ($timestr =~/^%S/)
- {
- print("$sec");
- }
- elsif ($timestr =~/^%w/)
- {
- print("$wday");
- }
- elsif ($timestr =~/^%y/)
- {
- print("$year");
- }
- elsif ($timestr =~/^%Z/)
- {
- print("$timezone");
- }
- elsif ($timestr =~/^%%/)
- {
- print("%");
- }
- else
- {
- if ($timestr =~ /^%./)
- {
- print("<b>$+</b>");
- }
- elsif ($timestr =~ /%/)
- {
- print("$`");
- $extra=join('','%',$');
- }
- }
- if (length($extra) > 0)
- {
- $timestr=$extra;
- }
- else
- {
- $timestr=$';
- }
- $len=length($timestr);
- }
- }
-